package com.sskj.wallet.ui.activity;


import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.widget.Button;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.sskj.common.adapter.ItemClickSupport;
import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.base.App;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.CoinEnum;
import com.sskj.lib.bean.WalletBean;
import com.sskj.lib.model.room.WalletBeanViewModel;
import com.sskj.lib.util.CopyUtil;
import com.sskj.wallet.R;
import com.sskj.wallet.R2;
import com.sskj.wallet.bean.WordBean;
import com.sskj.wallet.component.DaggerUserDataComponent;
import com.sskj.wallet.presenter.CheckWordActivityPresenter;
import com.sskj.wallet.uitl.TipWalletUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

import javax.inject.Inject;

import butterknife.BindView;
import io.reactivex.Flowable;

@Route(path = RConfig.WALLET_CHECK_WORD)//验证助记词
public class CheckWordActivity extends BaseActivity<CheckWordActivityPresenter> {
    @BindView(R2.id.tvWords)
    TextView tvWords;
    @BindView(R2.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R2.id.btSubmit)
    Button btSubmit;
    private List<WordBean> wordBeans = new ArrayList<>();
    @Inject
    WalletBeanViewModel walletBeanViewModel;
    private WalletBean walletBeanBtc;
    private WalletBean walletBeanEth;
    @Autowired
    String words;
    private WalletBean walletBean;

    @Override
    protected int getLayoutId() {
        return R.layout.wallet_activity_check_word;
    }

    @Override
    public CheckWordActivityPresenter getPresenter() {
        return new CheckWordActivityPresenter();
    }

    @Override
    protected void initView() {
        setTitle(App.INSTANCE.getString(R.string.wallet_checkWordActivity1_j));
        ARouter.getInstance().inject(this);
        DaggerUserDataComponent.create().inject(this);
        SlimAdapter slimAdapter = SlimAdapter.create().register(R.layout.wallet_recy_item_check_word, new SlimInjector<WordBean>() {
            @Override
            public void onInject(WordBean data, IViewInjector injector, List payloads) {

                injector
                        .text(R.id.tvWord, data.getWord())
                        .background(R.id.tvWord, ContextCompat.getDrawable(App.INSTANCE, data.isSelect() ? R.drawable.wallet_shape_dbc : R.drawable.wallet_shape_strockbdc));
            }
        }).attachTo(recyclerView).updateData(new ArrayList());
        List<String> strings = Arrays.asList(words.split(" "));
        Flowable.fromIterable(strings)
                .map(s -> new WordBean(s))
                .toList()
                .subscribe((wordBeans1, throwable) -> {

                    Random random = new Random(System.currentTimeMillis());
                    ArrayList<WordBean> strings1 = new ArrayList<>();
                    int size = wordBeans1.size();
                    for (int i = 0; i <size; i++) {
                        int index = random.nextInt(wordBeans1.size());
                        strings1.add(wordBeans1.get(index));
                        wordBeans1.remove(index);
                    }

                    slimAdapter.updateData(strings1);
                });
        ItemClickSupport.addTo(recyclerView)
                .setOnItemClickListener((recyclerView1, i, view) -> {
                    WordBean dataItem = (WordBean) slimAdapter.getDataItem(i);
                    dataItem.setSelect(!dataItem.isSelect());
                    slimAdapter.notifyItemChanged(i);
                    if (dataItem.isSelect()) {
                        wordBeans.add(dataItem);
                    } else {
                        wordBeans.remove(dataItem);
                    }
                    StringBuilder stringBuilder = new StringBuilder();
                    Flowable.fromIterable(wordBeans)
                            .map(WordBean::getWord)
                            .subscribe(s -> {
                                stringBuilder.append(s + " ");
                            }, e -> {
                            }, () -> {
                                if (stringBuilder.length() == 0) {
                                    tvWords.setText("");
                                } else {
                                    tvWords.setText(stringBuilder.substring(0, stringBuilder.length() - 1));
                                }
                            });
                });
        ClickUtil.click(btSubmit, () -> {
            if (!words.equals(tvWords.getText().toString())) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.wallet_checkWordActivity2_j));
                return;
            }
            TipWalletUtil.showBackupSuccess(this, () -> {
                walletBean.setBackup(true);
                walletBeanViewModel.insert(walletBean);
                finish();
            });
        });
    }

    @Override
    protected void initData() {


        walletBeanViewModel.getUsers().observe(this, walletBeans -> {
            if (walletBeans != null) {
                if (walletBeans.get(0).getCode().equals(CoinEnum.BTC.getCode())) {
                    walletBeanBtc = walletBeans.get(0);
                    walletBeanEth = walletBeans.get(1);
                    walletBean = walletBeanBtc;
                } else {
                    walletBeanBtc = walletBeans.get(1);
                    walletBeanEth = walletBeans.get(0);
                    walletBean = walletBeanEth;
                }
            }
        });
    }
}
